package cn.hpclub.server.controller.admin;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.Organization;
import cn.hpclub.server.service.OrganizationService;
import cn.hpclub.server.util.InterfaceUtil;
import cn.hpclub.server.validator.admin.OrganizationValidator;

public class OrganizationController extends BaseAdminController<Organization>{
    static String        UI_EDIT                   = "edit";                            // UI编辑标识
    static int           UI_PAGER_NUMBER_DEFAULT   = 1;                                 // UI默认起始页
    static int           UI_PAGER_SIZE_DEFAULT     = 10;                                // UI页容量
    public static String UI_SESSIONID              = "sid";                             // session
                                                                                        // id

    static String        HTML_ORGANIZATION_LIST    = "/admin/organization_list.tpl";    // 机构管理页
    static String        HTML_ORGANIZATION_EDIT    = "/admin/organization_edit.tpl";    // 机构编辑页
    static String        HTML_ORGANIZATION_PREVIEW = "/admin/organization_preview.tpl"; // 机构预览页

    public void list(){
        setAttr(Constant.Ui.PAGER,
                Organization.dao.list(getParaToInt(Constant.Ui.PAGER_NUMBER, UI_PAGER_NUMBER_DEFAULT),
                                      getParaToInt(Constant.Ui.PAGER_SIZE, UI_PAGER_SIZE_DEFAULT),
                                      getPara(Organization.NUMCODE, ""), getPara(Organization.DISPLAYNAME, ""),
                                      getPara(Organization.ORGTYPE, "-1")));

        setAttr(UI_SESSIONID, getSession().getId());
        this.keepPara(Organization.ORGTYPE);
        this.keepPara(Organization.NUMCODE);
        this.keepPara(Organization.DISPLAYNAME);
        /*
         * setAttr(Organization.dao.COLUMN_ORGANIZATION_ORGTYPE,
         * getPara(Organization.dao.COLUMN_ORGANIZATION_ORGTYPE, "-1"));
         */
        render(HTML_ORGANIZATION_LIST);

    }

    // 弹出层搜索
    public void query(){
        setAttr(Constant.Ui.PAGER,
                Organization.dao.reflash(getParaToInt(Constant.Ui.PAGER_NUMBER, 1),
                                         getParaToInt(Constant.Ui.PAGER_SIZE, 10), getPara(Organization.KEYWORD, ""),
                                         getPara("queryType", "fuzzy")));
        this.keepPara(Organization.KEYWORD);
        this.keepPara("queryType");
        render("/admin/org_dialog.tpl");
    }

    public void queryCity(){
        // System.out.println("themesid:" + getParaToInt("themesId"));
        setAttr(Constant.Ui.PAGER,
                Organization.dao.cityReflash(getParaToInt(Constant.Ui.PAGER_NUMBER, 1),
                                             getParaToInt(Constant.Ui.PAGER_SIZE, 10), getParaToInt("themesId"),
                                             getPara(Organization.KEYWORD, ""), getPara("queryType", "fuzzy")));
        setAttr("themesId", getPara("themesId"));
        this.keepPara(Organization.KEYWORD);
        this.keepPara("queryType");
        render("/admin/city_dialog.tpl");
    }

    // 弹出层模态list
    public void dialog(){
        setAttr(Constant.Ui.PAGER,
                Organization.dao.dialog(getParaToInt(Constant.Ui.PAGER_NUMBER, UI_PAGER_NUMBER_DEFAULT),
                                        getParaToInt(Constant.Ui.PAGER_SIZE, UI_PAGER_SIZE_DEFAULT), ""));
        this.keepPara(Organization.KEYWORD);
        render("/admin/org_dialog.tpl");
    }

    // 弹出层模态list
    public void cityDialog(){
        // System.out.println("themesid:" + getParaToInt("themesId"));
        setAttr(Constant.Ui.PAGER,
                Organization.dao.cityDialog(getParaToInt(Constant.Ui.PAGER_NUMBER, UI_PAGER_NUMBER_DEFAULT),
                                            getParaToInt(Constant.Ui.PAGER_SIZE, UI_PAGER_SIZE_DEFAULT),
                                            getParaToInt("themesId"), ""));
        setAttr("themesId", getParaToInt("themesId"));
        this.keepPara(Organization.KEYWORD);
        render("/admin/city_dialog.tpl");
    }

    public void preview(){
        setAttr(Constant.Ui.PAGER, Organization.dao.previewByNumcode(getPara(Organization.NUMCODE, "")));
        render(HTML_ORGANIZATION_PREVIEW);
    }

    /*
     * 分页
     */
    public void page(){
        this.keepPara("queryType", "fuzzy");
        renderJson(Organization.dao.reflash(getParaToInt(Constant.Ui.PAGER_NUMBER, UI_PAGER_NUMBER_DEFAULT),
                                            getParaToInt(Constant.Ui.PAGER_SIZE, UI_PAGER_SIZE_DEFAULT), "", "fuzzy"));
    }

    @Before({ OrganizationValidator.class, Tx.class })
    public void update(){
        Organization.dao.update(getParaMap());
        redirect("/organization/list");
    }

    /*
     * 批量删除
     */
    @Before({ Tx.class })
    public void delete(){
        if(Organization.dao.delete(getPara("ids"))){
            ajaxJsonSuccessMessage("删除成功");
        } else{
            ajaxJsonErrorMessage("删除失败");
        }
    }

    public void edit(){
        setAttr(Constant.Ui.PAGER, Organization.dao.preview(getPara(Organization.ID, "")));
        render(HTML_ORGANIZATION_EDIT);
    }

    public void tree(){
        this.renderText(OrganizationService.service.tree(InterfaceUtil.getIdTop()));
    }

    @Before({ Tx.class })
    public void load(){
        ajaxJsonSuccessJson(OrganizationService.service.load(getFile()));
    }
}
